using System;
using MONA.Modules.Marketing.BLL;

namespace MONA.Modules.Marketing.Controls
{
    public partial class AddEditCustomer : AddEditItemControl
    {
        public event EventHandler SaveClick;

        public bool Save()
        {
            int returnId = 0;
            if (Page.IsValid)
            {
                Customer customer = new Customer();
                customer.Name = txtCustomerName.Text.Trim();
                customer.Location = txtLocation.Text.Trim();
                customer.Description = txtDescription.Text.Trim();
                customer.ManagerID = Convert.ToInt32(ddlManager.SelectedValue);
                customer.ContactID = Convert.ToInt32(ddlContact.SelectedValue);
                customer.CustomerTypeID = Convert.ToInt32(ddlCustomerType.SelectedValue);
                customer.IsOwner = chkIsOwner.Checked;

                
                if (this.ItemID == 0)
                {
                    returnId = customer.Insert();
                }
                else
                {
                    customer.ID = this.ItemID;
                    returnId = customer.Update();
                }

                // Clear data in controls.            
                txtCustomerName.Text = txtLocation.Text = txtDescription.Text = string.Empty;
                ddlManager.SelectedIndex = ddlContact.SelectedIndex = ddlCustomerType.SelectedIndex = 0;
                chkIsOwner.Checked = false;
            }
            return returnId > 0;
        }


        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            UpdateBar.Visible = this.ShowUpdateBar;
            btnDelete.Visible = this.ItemID > 0;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                Customer customer = Customer.Load(this.ItemID);
                if (customer != null)
                {
                    txtCustomerName.Text = customer.Name;
                    txtLocation.Text = customer.Location;
                    txtDescription.Text = customer.Description;
                    ddlManager.SelectedValue = customer.ManagerID.ToString();
                    ddlContact.SelectedValue = customer.ContactID.ToString();
                    ddlCustomerType.SelectedValue = customer.CustomerTypeID.ToString();
                    chkIsOwner.Checked = customer.IsOwner;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.Save();
            if (SaveClick != null)
                SaveClick(sender, e);

            this.ReturnToReferrerPage();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.ReturnToReferrerPage();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                Customer customer = new Customer();
                customer.ID = this.ItemID;
                customer.Delete();
                this.ReturnToReferrerPage();
            }
        }
    }
}